<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

use OT\DataDictionary;

header("Content-type: text/html; charset=utf-8");

/**
 * 机构控制器
 */
class MechanismController extends HomeController
{

	/**
	 * 检测是否登录
	 * */
	public function _initialize()
	{
		if (empty(session('user_id'))) {
			header('Location: ' . U('/Home/Login/Login'));
			exit;
		} else {
			//再来判断权限
			if (session('Supertube') == 1) {
				//超管 拥有所有权限
			} else {
				$id = session('user_id');//不是超管的时候 user_id是account_number的主键ID
				$jurisdiction = M('account_number')->where("id=$id")->getField('jurisdiction');
				$action_name = ACTION_NAME;
				if ($action_name == setInfo || $action_name == SetInfo) {
					//访问数据统计
					$domain = strstr($jurisdiction, ',23,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				} else if ($action_name == account_authority || $action_name == Account_authority) {
					//排行榜
					$domain = strstr($jurisdiction, ',24,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				} else if ($action_name == faculty || $action_name == Faculty) {
					//线索列表
					$domain = strstr($jurisdiction, ',25,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				} else if ($action_name == feedback || $action_name == Feedback) {
					//线索列表
					$domain = strstr($jurisdiction, ',26,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				}
			}
		}
	}

	/**
	 * Mr.Jiang
	 * 师资力量
	 */
	public function faculty()
	{

		$uid = session('user_id');
		//当前用户所在的机构
		$institution = D('institution')->where(array('uid' => $uid))->find();
		$map['iid'] = array('eq', $institution['id']);
		$map['is_del'] = array('eq', 0);

		if ($_REQUEST['name'] != '') {
			$map['name'] = array('like', '%' . $_REQUEST['name'] . '%');
			$this->assign('name', $_REQUEST['name']);
		}

		if (!empty($_REQUEST['section_id'])) {
			$map['_string'] = "FIND_IN_SET(" . $_REQUEST['section_id'] . ",section_ids)";
			$this->assign('section_id', $_REQUEST['section_id']);
		}
		$count = M('teacher')->where($map)->count();
		$Page = new \Think\Page($count, 10, array('name' => $_REQUEST['name'], 'section_id' => $_REQUEST['section_id']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('teacher')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
		foreach ($list as $key => $item) {
			if ($item['section_ids'] != '') {
				$section_ids_str = mb_substr($item['section_ids'], mb_strlen($item['section_ids'], 'UTF-8') - 1, mb_strlen($item['section_ids'], 'UTF-8'), 'UTF-8');
				if ($section_ids_str == ',') {
					$item['section_ids'] = mb_substr($item['section_ids'], 0, mb_strlen($item['section_ids'], 'UTF-8') - 1, 'UTF-8');
				}
				$section_ids_arr = explode(",", $item['section_ids']);

				$section_name = "";
				for ($i = 0; $i < count($section_ids_arr); $i++) {
					$name = D('section_teacher')->where(array('id' => $section_ids_arr[$i]))->getField('name');
					$section_name .= $name ? $name . "," : '';
				}
				$list[$key]['section_name'] = $section_name;
			} else {
				$list[$key]['section_name'] = "";
			}
		}

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$section = D('section_teacher')->where(array('iid' => $institution['id']))->select();
		$this->assign('section', $section);
		$this->assign('cur', '师资力量');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 添加师资力量
	 */
	public function addFaculty()
	{
		$uid = session('user_id');
		$memberInfo = D('ucenter_member')->where(array('id' => $uid))->find();
		if (IS_POST) {
			// if ($memberInfo['user_type'] != 3) {
			//     $this->error("抱歉，只有机构负责人才能添加师资力量");
			// }

			$data['name'] = $_REQUEST['name'];
			$data['tag'] = $_REQUEST['tag'];
			$data['desc'] = $_REQUEST['desc'];
			$data['avatar_id'] = $_REQUEST['avatar_id'];
			$data['cover_id'] = $_REQUEST['cover_id'];
			$section_ids_arr = $_REQUEST['section_ids'];
			$data['section_ids'] = "";
			foreach ($section_ids_arr as $key => $item) {
				$data['section_ids'] .= $section_ids_arr[$key] . ",";
			}

			$data['sign'] = $_REQUEST['sign'];

			if ($data['name'] == '') {
				$this->error("请输入姓名");
			}

			if ($data['section_ids'] == '') {
				$this->error("请选择板块");
			}

			if (empty($data['avatar_id'])) {
				$this->error("请上传头像");
			}

			if (empty($data['cover_id'])) {
				$this->error("请上传封面");
			}

			$ucenterMemberData['username'] = $data['name'];
			$ucenterMemberData['password'] = think_ucenter_md5('123456', '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT');
			$ucenterMemberData['iid'] = $memberInfo['iid'];
			$time = time();
			$ucenterMemberData['reg_time'] = $time;
			$ucenterMemberData['user_type'] = 4;
			$ucenterMemberData['update_time'] = $time;
			$ucenterMemberData['status'] = 1;
			$result = D('ucenter_member')->add($ucenterMemberData);
			if ($result) {
				$memberData['uid'] = $result;
				$memberData['nickname'] = $data['name'];
				$memberData['reg_time'] = $time;
				$memberData['status'] = 1;
				$res = D('member')->add($memberData);
				if ($res) {
					$data['uid'] = $result;
					$data['iid'] = $memberInfo['iid'];
					$data['create_time'] = $time;
					$data['update_time'] = $time;
					$resu = D('teacher')->add($data);
					if ($resu) {
						$this->success("添加成功", U('Home/Mechanism/faculty'));
					} else {
						$this->error("添加失败");
					}
				} else {
					$this->error("添加失败");
				}
			} else {
				$this->error("添加失败");
			}
		} else {
			// $section = D('section_teacher')->where(array('iid'=>$memberInfo['iid']))->select();
			$section = D('section_teacher')->select();
			$this->assign('section', $section);
			$this->assign('cur', '师资力量');
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 修改师资力量
	 */
	public function editFaculty()
	{
		$uid = session('user_id');
		if (IS_POST) {
			$memberInfo = D('ucenter_member')->where(array('id' => $uid))->find();
			if ($memberInfo['user_type'] != 3) {
				return api_return(2, '抱歉，只有机构负责人才能编辑师资力量');
			}

			$id = $_REQUEST['id'];
			$data['name'] = $_REQUEST['name'];
			$data['tag'] = $_REQUEST['tag'];
			$data['desc'] = $_REQUEST['desc'];
			$data['avatar_id'] = $_REQUEST['avatar_id'];
			$data['cover_id'] = $_REQUEST['cover_id'];
			$section_ids_arr = $_REQUEST['section_ids'];
			$data['section_ids'] = "";
			foreach ($section_ids_arr as $key => $item) {
				$data['section_ids'] .= $section_ids_arr[$key] . ",";
			}

			$data['sign'] = $_REQUEST['sign'];

			if ($data['name'] == '') {
				return api_return(2, '请输入姓名');
			}

			if ($data['section_ids'] == '') {
				return api_return(2, '请选择板块');
			}

			if (empty($data['avatar_id'])) {
				$this->error("请上传头像");
			}

			if (empty($data['cover_id'])) {
				$this->error("请上传封面");
			}

			$time = time();
			$data['update_time'] = $time;
			$resu = D('teacher')->where(array('id' => $id))->save($data);
			if ($resu) {
				$member_id = D('teacher')->where(array('id' => $id))->getField('uid');
				D('ucenter_member')->where(array('id' => $member_id))->save(array('username' => $data['name'], 'update_time' => $time));
				D('member')->where(array('uid' => $member_id))->save(array('nickname' => $data['name']));
				$this->success("编辑成功", U('Home/Mechanism/faculty'));
			} else {
				$this->error("编辑失败");
			}
		} else {
			$id = $_REQUEST['id'];
			$info = D('teacher')->where(array('id' => $id))->find();
			$info['avatar_img'] = D('picture')->where(array('id' => $info['avatar_id']))->getField('path');
			$info['cover_img'] = D('picture')->where(array('id' => $info['cover_id']))->getField('path');
			$this->assign('info', $info);
			$str = mb_substr($info['section_ids'], mb_strlen($info['section_ids'], 'UTF-8') - 1, mb_strlen($info['section_ids'], 'UTF-8'), 'UTF-8');
			if ($str == ",") {
				$info['section_ids'] = mb_substr($info['section_ids'], 0, mb_strlen($info['section_ids'], 'UTF-8') - 1, 'UTF-8');
			}
			$section_ids_arr = explode(',', $info['section_ids']);
			$section = D('section_teacher')->where(array('iid' => $info['iid']))->select();
			foreach ($section as $key => $item) {
				for ($i = 0; $i < count($section_ids_arr); $i++) {
					if ($item['id'] == $section_ids_arr[$i]) {
						$section[$key]['checked'] = 1;
					}
				}
			}
			$this->assign('section', $section);
			$this->assign('cur', '师资力量');
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 删除师资力量
	 */
	public function delFaculty()
	{
		$id = $_REQUEST['id'];
		$count = D('teacher')->where(array('id' => $id, 'is_del' => 0))->count();
		if ($count <= 0) {
			$this->error("该数据不存在或已被删除");
		}

		$result = D('teacher')->where(array('id' => $id, 'is_del' => 0))->save(array('is_del' => 1));
		if ($result) {
			$this->success("删除成功");
		} else {
			$this->error("删除失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 意见反馈
	 */
	public function feedback()
	{
		if (IS_POST) {
			$data['uid'] = session('user_id');
			$type_name_arr = $_REQUEST['type_name'];
			$data['type_name'] = "";
			foreach ($type_name_arr as $key => $item) {
				$data['type_name'] .= $type_name_arr[$key] . ",";
			}
			$data['content'] = $_REQUEST['content'];
			$data['type'] = 2;
			$time = time();
			$data['create_time'] = $time;
			$data['update_time'] = $time;
			$result = D('feedback')->add($data);
			if ($result) {
				$this->success("提交成功");
			} else {
				$this->error("提交失败");
			}
		} else {
			$tags = D('feedback_tag')->order('id desc')->select();
			$this->assign('tags', $tags);
			$this->assign('cur', '意见反馈');
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 信息设置
	 */
	public function setInfo()
	{
		$uid = session("user_id");
		$menu = $_REQUEST['menu'];
		if (IS_POST) {
			if ($menu == 1) {
				$institution_info = D('institution')->where(array('uid' => $uid))->find();
//				$data['name'] = $_REQUEST['name'];
				$data['username'] = $_REQUEST['username'];
				$data['mobile'] = $_REQUEST['mobile'];
				$data['IDcard'] = $_REQUEST['IDcard'];
				$data['card_front'] = $_REQUEST['card_front'];
				$data['card_behind'] = $_REQUEST['card_behind'];
				$data['hold_card'] = $_REQUEST['hold_card'];
//				$data['business_license'] = $_REQUEST['business_license'];
				$data['bank_id'] = $_REQUEST['bank_id'];
				$data['bank_num'] = $_REQUEST['bank_num'];

//				if ($data['name'] == '') {
//					$this->error("请输入机构名称");
//				}

				if ($data['username'] == '') {
					$this->error("请输入负责人姓名");
				}

				if ($data['mobile'] == '') {
					$this->error("请输入负责人联系方式");
				}
				if ($_REQUEST['code'] == '') $this->error("请输入验证码");

				if ($data['IDcard'] == '') {
					$this->error("请输入负责人身份证号码");
				}
				if (empty($data['bank_id'])) $this->error('请选择开户行');
				if (empty($data['bank_num'])) $this->error('请填写银行卡号');

				if (empty($data['card_front'])) {
					$this->error("请上传身份证正面");
				}

				if (empty($data['card_behind'])) {
					$this->error("请上传身份证反面");
				}

				if (empty($data['hold_card'])) {
					$this->error("请上传手持身份证");
				}

//				if (empty($data['business_license'])) {
//					$this->error("请上传营业执照");
//				}
				$model = M();
				$model->startTrans();

				$time = time();
				$data['update_time'] = $time;
				if (empty($institution_info)) {
					$data['uid'] = $uid;
					$data['create_time'] = $time;
					$result = $model->table('teach_institution')->add($data);
				} else {
					$result = $model->table('teach_institution')->where(array('uid' => $uid))->save($data);
				}

				Vendor('pay.pay');

				$user_info = array(
					'user_name' => $data['username'],
					'cert_id' => $data['IDcard'],
					'user_mobile' => $data['mobile'],
					'bank_id' => $data['bank_id'],
					'card_no' => $data['bank_num'],
					'vali_date' => '99991231'
				);

				if ($institution_info['sub_mch_code']) {
					$status_data = array(
						'sub_mch_code' => $institution_info['sub_mch_code']
					);

					$status_obj = new \pay($status_data);
					$status_info = $status_obj->status();
					if ($status_info['data']['status'] != 2) {
						/***************上传身份证正反面，手持身份证******************/
						//身份证正面图片
						$card_front_path = M('picture')->where(array('id' => $data['card_front']))->getField('path');
						$card_behind_path = M('picture')->where(array('id' => $data['card_behind']))->getField('path');
						$card_info = array(
							'identity_front' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $card_front_path)),
							'identity_contrary' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $card_behind_path)),
						);

						//手持身份证图片
						$hold_card_path = M('picture')->where(array('id' => $data['hold_card']))->getField('path');
						$hold_card_info = array(
							'person_with_id_photo' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $hold_card_path))
						);
						uploadCardInfo($institution_info['sub_mch_code'], $user_info, $card_info, $hold_card_info);
						/***************上传身份证正反面，手持身份证******************/
					}
				} else {
					if ($_REQUEST['sms_id'] && $_REQUEST['code']) {
						$obj_data = array(
							'user_info' => json_encode($user_info),
							'merchant_type' => 2
						);
						$obj = new \pay($obj_data);
						$merchant_info = $obj->merchant($_REQUEST['code'], $_REQUEST['sms_id']);

						if ($merchant_info['result'] == 1) {
							//这个地方merchant_code就是sub_mch_code
							$sub_mch_code = $merchant_info['data']['merchant_code'];

							$code_res = $model->table('teach_institution')->where(array('id' => $institution_info['id']))->setField('sub_mch_code', $sub_mch_code);

							if ($code_res) {
								/***************上传身份证正反面，手持身份证******************/
								//身份证正面图片
								$card_front_path = M('picture')->where(array('id' => $data['card_front']))->getField('path');
								$card_behind_path = M('picture')->where(array('id' => $data['card_behind']))->getField('path');
								$card_info = array(
									'identity_front' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $card_front_path)),
									'identity_contrary' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $card_behind_path)),
								);

								//手持身份证图片
								$hold_card_path = getPicture($data['hold_card']);
								$hold_card_info = array(
									'person_with_id_photo' => str_replace(array("\r\n", "data:image/jpg;base64,", "data:image/png;base64,"), "", imgToBase64($_SERVER['DOCUMENT_ROOT'] . $hold_card_path))
								);
								uploadCardInfo($sub_mch_code, $user_info, $card_info, $hold_card_info);
								/***************上传身份证正反面，手持身份证******************/
							}
						}else{
                            $this->error($merchant_info['message']);
                        }
					}
				}

				if ($result) {
					$model->commit();
					$this->success("保存成功");
				} else {
					$model->rollback();
					$this->error("保存失败");
				}
			} else if ($menu == 2) {
				$old_password = $_REQUEST['old_password'];
				$new_password = $_REQUEST['new_password'];
				$confirm_password = $_REQUEST['confirm_password'];

				if ($old_password == '') {
					$this->error("请输入原密码");
				}
				$member = D('ucenter_member')->where(array('id' => $uid))->find();
				if (think_ucenter_md5($old_password, '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT') != $member['password']) {
					$this->error("原密码不正确");
				}

				if ($new_password == '') {
					$this->error("请输入新密码");
				}

				if ($confirm_password != $new_password) {
					$this->error("确认密码与新密码不一致");
				}

				if ($new_password == $old_password) {
					$this->error("新密码与原密码不能一致");
				}

				$result = D('ucenter_member')->where(array('id' => $uid))->save(array('password' => think_ucenter_md5($new_password, '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT')));
				if ($result) {
					$this->success("修改成功");
				} else {
					$this->error("修改失败");
				}
			} else {
				$count = D('institution')->where(array('uid' => $uid))->count();
				if ($count <= 0) {
					$this->error("请先认证");
				}
				$data['logo'] = $_REQUEST['logo'];
				$data['name'] = $_REQUEST['name'];
				$data['desc'] = $_REQUEST['desc'];
				$banner_one = $_REQUEST['banner_one'];
				$banner_two = $_REQUEST['banner_two'];
				$banner_three = $_REQUEST['banner_three'];

				$data['banner'] = "";
				if (!empty($banner_one)) {
					$data['banner'] .= $banner_one . ",";
				}
				if (!empty($banner_two)) {
					$data['banner'] .= $banner_two . ",";
				}
				if (!empty($banner_three)) {
					$data['banner'] .= $banner_three . ",";
				}
				if ($data['banner'] != '') {
					$data['banner'] = mb_substr($data['banner'], 0, mb_strlen($data['banner'], 'UTF-8') - 1, 'UTF-8');
				}

				if (empty($data['logo'])) {
					$this->error("请上传logo");
				}

				if ($data['name'] == '') {
					$this->error("请输入机构名称");
				}

				if ($data['desc'] == '') {
					$this->error("请输入机构介绍");
				}

				$data['update_time'] = time();
				$result = D('institution')->where(array('uid' => $uid))->save($data);
				if ($result) {
					$this->success("保存成功");
				} else {
					$this->error("保存失败");
				}
			}
		} else {
			$info = D('institution')->where(array('uid' => $uid))->find();
			$info['logo_img'] = D('picture')->where(array('id' => $info['logo']))->getField('path');
			$info['card_front_path'] = D('picture')->where(array('id' => $info['card_front']))->getField('path');
			$info['card_behind_path'] = D('picture')->where(array('id' => $info['card_behind']))->getField('path');
			$info['business_license_path'] = D('picture')->where(array('id' => $info['business_license']))->getField('path');
			$info['hold_card_path'] = getPicture($info['hold_card']);

			$banner_str = mb_substr($info['banner'], mb_strlen($info['banner'], 'UTF-8') - 1, mb_strlen($info['banner'], 'UTF-8'), 'UTF-8');
			if ($banner_str == ',') {
				$info['banner'] = mb_substr($info['banner'], 0, mb_strlen($info['banner'], 'UTF-8') - 1, 'UTF-8');
			}
			$banner_arr = explode(',', $info['banner']);
			for ($i = 0; $i < count($banner_arr); $i++) {
				$info['banners'][$i]['id'] = $banner_arr[$i];
				$info['banners'][$i]['img'] = D('picture')->where(array('id' => $banner_arr[$i]))->getField('path');
			}

			//图库
			// $gallery = M('picture_gallery a')->field("a.*,b.name")->join(" left join teach_picture_gallery_type b on a.type_id = b.id")->order('a.create_time desc')->select();
			$condition['a.style'] = array('eq', 0);
			$gallery = M('picture_gallery a')->field("a.*,b.name")->join(" left join teach_picture_gallery_type b on a.type_id = b.id")->where($condition)->order('a.create_time desc')->select();
			foreach ($gallery as $key => $item) {
				$path = D('picture')->where(array('id' => $item['img']))->getField('path');
				$gallery[$key]['img_path'] = $path ? $path : '';
			}

			$bank_list = M('bank')->order('bank_id asc')->select();
			$this->assign('bank_list', $bank_list);

			Vendor('pay.pay');

			$obj_data = array(
				'sub_mch_code' => $info['sub_mch_code']
			);
			$obj = new \pay($obj_data);
			$status_info = $obj->status();
			$upload_status = $status_info['data']['status'];
			$this->assign('upload_status', $upload_status);
			$this->assign('gallery', $gallery);
			$this->assign('info', $info);
			$this->assign('menu', $menu);
			$this->assign('cur', '信息设置');
			$this->display();
		}
	}

	public function sendSms()
	{
		$input = I('post.');

		$mobile = $input['mobile'];

		if (empty($mobile)) api_return(2, '请输入联系手机');

		Vendor('pay.pay');

		$data = array(
			'user_mobile' => $mobile,
		);
		$obj = new \pay($data);
		$sms_result = $obj->sms();
		if ($sms_result['result'] == 1) {
			api_return(1, '获取数据成功', $sms_result['data']['sms_id']);
		} else {
			api_return(2, '短信发送失败');
		}
	}

	/**
	 * Mr.Jiang
	 * 图库列表
	 */
	public function pictureGalleryList()
	{
		if (IS_POST) {
			$name = $_REQUEST['name'];
			if ($name != '') {
				$map['b.name'] = array('like', '%' . $_REQUEST['name'] . '%');
			}
			$map['a.style'] = array('eq', 0);//
			$list = M('picture_gallery a')->field("a.*,b.name")->join(" left join teach_picture_gallery_type b on a.type_id = b.id")->where($map)->order('a.create_time desc')->select();
			if ($list) {
				foreach ($list as $key => $item) {
					$path = D('picture')->where(array('id' => $item['img']))->getField('path');
					$list[$key]['img_path'] = $path ? $path : '';
				}
			}
			$list = $list ? $list : [];

			return api_return(1, '信息获取成功', $list);
		}
	}

	//账号权限
	public function account_authority()
	{
		$map['iid'] = session('iid');
		$_REQUEST['iid'] = session('iid');
		$_REQUEST['mobile'] ? $map['mobile'] = array('like', "%$_REQUEST[mobile]%") : '';
		$this->assign('mobile', $_REQUEST['mobile']);
		$count = M('teacher')->where($map)->count();
		$Page = new \Think\Page($count, 10, $_REQUEST);
		$list = M('account_number')->limit($Page->firstRow . ',' . $Page->listRows)->where($map)->select();
		foreach ($list as $k => $v) {
			$bb = substr($v['jurisdiction'], 1);//去掉第一个逗号
			$jurisdiction = explode(',', $bb);
			foreach ($jurisdiction as $kk => $vv) {
				if ($vv == 1) {
					$list[$k]['sub'] .= ',' . '数据统计';
				} else if ($vv == 2) {
					$list[$k]['sub'] .= ',' . '排行榜';
				} else if ($vv == 3) {
					$list[$k]['sub'] .= ',' . '线索列表';
				} else if ($vv == 4) {
					$list[$k]['sub'] .= ',' . '客户列表';
				} else if ($vv == 5) {
					$list[$k]['sub'] .= ',' . '跟进记录';
				} else if ($vv == 6) {

					$list[$k]['sub'] .= ',' . '学习统计';
				} else if ($vv == 7) {
					$list[$k]['sub'] .= ',' . '学生管理';
				} else if ($vv == 8) {
					$list[$k]['sub'] .= ',' . '班级分类';
				} else if ($vv == 9) {
					$list[$k]['sub'] .= ',' . '班级列表';
				} else if ($vv == 10) {
					$list[$k]['sub'] .= ',' . '在线成绩';
				} else if ($vv == 11) {
					$list[$k]['sub'] .= ',' . '错题汇总';
				} else if ($vv == 12) {
					$list[$k]['sub'] .= ',' . '财务统计';
				} else if ($vv == 13) {
					$list[$k]['sub'] .= ',' . '交易管理';
				} else if ($vv == 14) {
					$list[$k]['sub'] .= ',' . '收费管理';
				} else if ($vv == 15) {
					$list[$k]['sub'] .= ',' . '内容统计';
				} else if ($vv == 16) {
					$list[$k]['sub'] .= ',' . '直播课管理';
				} else if ($vv == 17) {
					$list[$k]['sub'] .= ',' . '点播课管理';
				} else if ($vv == 18) {
					$list[$k]['sub'] .= ',' . '面授课管理';
				} else if ($vv == 19) {
					$list[$k]['sub'] .= ',' . '课程拼团';
				} else if ($vv == 20) {
					$list[$k]['sub'] .= ',' . '课程秒杀';
				} else if ($vv == 21) {
					$list[$k]['sub'] .= ',' . '优惠券管理';
				} else if ($vv == 22) {
					$list[$k]['sub'] .= ',' . '代购记录';
				} else if ($vv == 23) {
					$list[$k]['sub'] .= ',' . '信息设置';
				} else if ($vv == 24) {
					$list[$k]['sub'] .= ',' . '账号权限';
				} else if ($vv == 25) {
					$list[$k]['sub'] .= ',' . '师资力量';
				} else if ($vv == 26) {
					$list[$k]['sub'] .= ',' . '意见反馈';
				}
			}
			$list[$k]['sub'] = substr($list[$k]['sub'], 1);
		}
		// print_r($list);die;
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);
		$this->assign('list', $list);
		$this->assign('cur', '账号权限');
		$this->display();
	}

	//新增账号
	public function account_add()
	{
		if ($_POST) {
			// print_r($_REQUEST);die;
			$add_data['password'] = think_ucenter_md5($_REQUEST['password'], '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT');
			$add_data['name'] = $_REQUEST['name'];
			$add_data['mobile'] = $_REQUEST['mobile'];
			$jurisdiction = $_REQUEST['checked'];
			foreach ($jurisdiction as $k => $v) {
				$jur .= ',' . $v;
			}
			// $add_data['jurisdiction']=substr($jur, 1);//后面觉得还是前面有个逗号好
			$add_data['jurisdiction'] = $jur;
			$add_data['iid'] = session('iid');
			$res = M('account_number')->add($add_data);
			if ($res) {
				return api_return(1, '成功');
			} else {
				return api_return(2, '失败');
			}
		}
		$this->assign('cur', '账号权限');
		$this->display();
	}

	public function account_edit()
	{
		if ($_POST) {
			// print_r($_REQUEST);die;
			if (strlen($info['password']) > 30) {
				//还是加密之后的密码 没有改
				$add_data['password'] = $_REQUEST['password'];
			} else {
				//是重新输入的密码 要重新加密    这种判断不严谨，后面可以改善
				$add_data['password'] = think_ucenter_md5($_REQUEST['password'], '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT');
			}

			$add_data['name'] = $_REQUEST['name'];
			$add_data['mobile'] = $_REQUEST['mobile'];
			$jurisdiction = $_REQUEST['checked'];
			foreach ($jurisdiction as $k => $v) {
				$jur .= ',' . $v;
			}
			// $add_data['jurisdiction']=substr($jur, 1);//后面觉得还是前面有个逗号好
			$add_data['jurisdiction'] = $jur;
			$add_data['iid'] = session('iid');
			$add_data['id'] = $_REQUEST['id'];
			$res = M('account_number')->save($add_data);
			if ($res) {
				return api_return(1, '成功');
			} else {
				return api_return(2, '失败');
			}
		}
		$id = $_REQUEST['id'];
		$info = M('account_number')->where("id=$id")->find();

		$jur = substr($info['jurisdiction'], 1);
		$jurisdiction = explode(',', $jur);
		foreach ($jurisdiction as $k => $v) {
			if ($v == 1) {
				$a = 1;
			} else if ($v == 2) {
				$b = 2;
			} else if ($v == 3) {
				$c = 3;
			} else if ($v == 4) {
				$d = 4;
			} else if ($v == 5) {
				$e = 5;
			} else if ($v == 6) {
				$f = 6;
			} else if ($v == 7) {
				$g = 7;
			} else if ($v == 8) {
				$h = 8;
			} else if ($v == 9) {
				$i = 9;
			} else if ($v == 10) {
				$j = 10;
			} else if ($v == 11) {
				$ka = 11;
			} else if ($v == 12) {
				$l = 12;
			} else if ($v == 13) {
				$m = 13;
			} else if ($v == 14) {
				$n = 14;
			} else if ($v == 15) {
				$o = 15;
			} else if ($v == 16) {
				$p = 16;
			} else if ($v == 17) {
				$q = 17;
			} else if ($v == 18) {
				$r = 18;
			} else if ($v == 19) {
				$s = 19;
			} else if ($v == 20) {
				$t = 20;
			} else if ($v == 21) {
				$u = 21;
			} else if ($v == 22) {
				$va = 22;
			} else if ($v == 23) {
				$w = 23;
			} else if ($v == 24) {
				$x = 24;
			} else if ($v == 25) {
				$y = 25;
			} else if ($v == 26) {
				$z = 26;
			}
		}
		$this->assign('a', $a);
		$this->assign('b', $b);
		$this->assign('c', $c);
		$this->assign('d', $d);
		$this->assign('e', $e);
		$this->assign('f', $f);
		$this->assign('g', $g);
		$this->assign('h', $h);
		$this->assign('i', $i);
		$this->assign('j', $j);
		$this->assign('k', $ka);
		$this->assign('l', $l);
		$this->assign('m', $m);
		$this->assign('n', $n);
		$this->assign('o', $o);
		$this->assign('p', $p);
		$this->assign('q', $q);
		$this->assign('r', $r);
		$this->assign('s', $s);
		$this->assign('t', $t);
		$this->assign('u', $u);
		$this->assign('v', $va);
		$this->assign('w', $w);
		$this->assign('x', $x);
		$this->assign('y', $y);
		$this->assign('z', $z);
		$this->assign('jurisdiction', $jurisdiction);
		$this->assign('info', $info);
		$this->assign('cur', '账号权限');
		$this->display();
	}

	//操作日志
	public function account_log()
	{
		$map['account_id'] = $_REQUEST['id'];
		if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
			$btime = strtotime($_REQUEST['start_time']);
			$etime = strtotime($_REQUEST['end_time']);
			$map['create_time'] = array('between', "$btime,$etime");
			$this->assign('start_time', $_REQUEST['start_time']);
			$this->assign('end_time', $_REQUEST['end_time']);
		}
		$count = M('account_log')->where($map)->count();
		$Page = new \Think\Page($count, 10, $_REQUEST);
		// print_r($map);die;
		$list = M('account_log')->limit($Page->firstRow . ',' . $Page->listRows)->where($map)->select();
		foreach ($list as $k => $v) {
			$list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
		}
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);
		$this->assign('list', $list);
		$this->assign('cur', '账号权限');
		$this->display();
	}

	public function account_del()
	{
		$id = $_REQUEST['id'];
		$res = M('account_number')->where("id=$id")->delete();
		if ($res) {
			$this->success('成功', U('account_log'));
		} else {
			$this->error('失败');
		}
	}

	//账户设置
	public function account_manage123()
	{
		$iid = session('iid');
		if ($_POST) {
			$re = M('institution_bank_info')->where("iid=$iid")->find();
			if ($re) {
				//存在就修改
				$data = $_POST;
				$data['update_time'] = time();
				$res = M('institution_bank_info')->where("iid=$iid")->save($data);
			} else {
				//不存在就添加
				$data = $_POST;
				$data['create_time'] = time();
				$data['iid'] = $iid;
				$res = M('institution_bank_info')->add($data);
			}
			if ($res) {
				return $this->success('成功', U('Mechanism/account_manage'));
			} else {
				return $this->error('失败', U('Mechanism/account_manage'));
			}
		}

		$info = M('institution_bank_info')->where("iid=$iid")->find();
		$this->assign('info', $info);

		$this->assign('cur', '账户设置');
		$this->display();
	}

	public function account_manage()
	{
		$iid = session('iid');
		if ($_POST) {
			//存在就修改
			$data = $_POST;
			$data['update_time'] = time();
			$res = M('institution')->where("id=$iid")->save($data);

			if ($res) {
				return $this->success('成功', U('Mechanism/account_manage'));
			} else {
				return $this->error('失败', U('Mechanism/account_manage'));
			}
		}

		$info = M('institution')->where("id=$iid")->find();
		$this->assign('info', $info);

		$this->assign('cur', '账户设置');
		$this->display();
	}
}
